package com.example.mallapi.service.impl;/*
@author name
@data 2023/5/31-15:14
*/

import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.mallapi.common.Constants;
import com.example.mallapi.entity.Admin;
import com.example.mallapi.exception.BusinessException;
import com.example.mallapi.mapper.AdminMapper;
import com.example.mallapi.qo.LoginQo;
import com.example.mallapi.service.LoginService;
import com.example.mallapi.utils.RedisUtils;
import com.example.mallapi.utils.VerifyCodeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

@Service
public class LoginServiceImpl implements LoginService {

    @Autowired
    private RedisUtils redisUtils;

    @Autowired
    private AdminMapper adminMapper;

    @Override
    public Map<String, String> getCaptcha() {
        Map<String, String> map = VerifyCodeUtil.generateVerifyCode();
        //从map获取验证码，后存储到redis中，并设置过期时间
        String code = map.get("code");
        String uuid = map.get("uuid");
        redisUtils.set(Constants.LOGIN_CAPTCHA_CODE+uuid,code,Constants.LOGIN_CAPTCHA_CODE_EXPIRE_TIME);

        //将map中的验证码删除
        map.remove("code");
        return map;
    }

    @Override
    public Map<String, String> login(LoginQo qo) {
        //校验验证码是否正确
        String code = qo.getCode();
        String trueCode = redisUtils.get(Constants.LOGIN_CAPTCHA_CODE + qo.getUuid());

        //校验验证码一致性
        if(!VerifyCodeUtil.verification(trueCode,code,true)){
            throw new BusinessException("验证码错误");
//            return Result.fail(501,"验证码错误");
        }

        //用户是存在
        LambdaQueryWrapper<Admin> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Admin::getAdminName,qo.getUsername())
                .eq(Admin::getAdminPassword,qo.getPassword());
        Admin loginAdmin = adminMapper.selectOne(wrapper);

        //如果loginAdmin 如过为null，说明登录失败
        if(loginAdmin == null){
            throw new BusinessException("密码或账户输入失败");
        }

        //登录成功，生成token
        String token = UUID.randomUUID().toString().replaceAll("-", "");

        //将登录的用户信息存储到redis中，token作为key，登录的用户对象作为value
        redisUtils.set(Constants.LOGIN_ADMIN_INFO_TOKEN + token,JSONUtil.toJsonStr(loginAdmin),Constants.LOGIN_ADMIN_INFO_TOKEN_EXPIRE_TIME);


        HashMap<String, String> map = new HashMap<>();
        map.put("token",token);

        return map;
    }

    @Override
    public Admin getUserInfo(HttpServletRequest request) {
        //获取用户信息逻辑
        //获取请求头的token
        String token = request.getHeader("Authorization");
        //从redis获取用户信息
        String adminJson = redisUtils.get(Constants.LOGIN_ADMIN_INFO_TOKEN + token);
        //将用户信息的json数据 转成对象
        Admin admin = JSONUtil.toBean(adminJson, Admin.class);
        //密码清空
        admin.setAdminPassword(null);

        return admin;
    }

    public void logout(HttpServletRequest request){

        String token = request.getHeader("Authorization");

        //将redis中token删除
        redisUtils.del(Constants.LOGIN_ADMIN_INFO_TOKEN + token);
    }
}
